<template>
  <div>我是登录</div>

  <el-form :model="loginForm" label-width="120px">
    <el-form-item label="账号">
      <el-input v-model="loginForm.account" placeholder="请输入用户名或邮箱" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitLoginForm">登录</el-button>
    </el-form-item>
  </el-form>

</template>
  
<script>
import { ElMessage } from 'element-plus'
import { req_login } from '@/api/index.js'
import {dict2ElMessage, response2ElMessage} from '@/utils/response-utils'

export default {
  name: 'LoginView',
  props: {

  },
  data() {
    return {
      loginForm: {
        account: "",
        password: ""
      }
    }
  },
  methods: {
    async submitLoginForm() {
      // 前端检验
      if (this.loginForm.account === '') {
        ElMessage.info("请输入账号！")
        return
      }
      if (this.loginForm.password === '') {
        ElMessage.info("请输入密码！")
        return
      }

      // 发送请求
      let response = await req_login(this.loginForm.account, this.loginForm.password)
      let data = response.data
      let user = data['user']
      console.log(response)

      // 更新全局状态
      if (data['errno'] === 1) {
        this.$store.commit('setUser', {
          user_id: user['user_id'],
          user_name: user['user_name'],
          email: user['email'],
        })

        // 回应
        dict2ElMessage(response, {
          "showElMsg": true,
          "msg": "你好，" + user['user_name'] + "！",
          "msgType": "success",
          "outputToConsole": false
        })

        // 跳转
        this.$router.push({
          path: '/'
        })
      }
      else {
        // 回应
        response2ElMessage(response, {
          1003: {
            "showElMsg": true,
            "msg": "当前已登录！",
            "msgType": "warning",
            "outputToConsole": false
          }
        })
      }

    }
  },
  created() {

  },
  mounted() {

  }
}
</script>
  